package com.example.dbdesign.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 配置所有路径都支持跨域
                .allowedOrigins("*")  // 允许所有来源的请求
                .allowedMethods("*")  // 允许所有 HTTP 方法（GET、POST、PUT 等）
                .allowedHeaders("*")  // 允许所有请求头
                .maxAge(3600);  // 设置跨域请求的缓存时间为 3600 秒
    }

    @Value("${upload.UploadPath}")
    private String uploadPath;

    @Value("${upload.AccessPath}")
    private String accessPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(accessPath + "**").addResourceLocations("file:" + uploadPath);
    }
}
